package com.atguigu.tingshu.user.api;


import com.atguigu.tingshu.common.login.GuiguLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserPaidAlbum;
import com.atguigu.tingshu.model.user.UserPaidTrack;
import com.atguigu.tingshu.model.user.UserSubscribe;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.user.service.UserPaidAlbumService;
import com.atguigu.tingshu.user.service.UserPaidTrackService;
import com.atguigu.tingshu.user.service.UserSubscribeService;
import com.atguigu.tingshu.user.strategy.paid.PaidRecordContext;
import com.atguigu.tingshu.user.strategy.paid.PaidRecordStrategy;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Set;
import java.util.stream.Collectors;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user/userInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class UserInfoApiController {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserPaidTrackService userPaidTrackService;
    @Autowired
    private UserPaidAlbumService userPaidAlbumService;
    @Autowired
    private UserSubscribeService userSubscribeService;
    @Autowired
    private PaidRecordContext paidRecordContext;
    @Operation(summary = "内部接口： 查询订单是否支持退款")
    @GuiguLogin
    @GetMapping("isSupportRefund/{orderNo}")
    public Result<Boolean> isSupportRefund(@PathVariable("orderNo")String orderNo){
        return Result.ok(userInfoService.isSupportRefund(orderNo));
    }
    @Operation(summary = "内部接口：保存支付成功的交易记录")
    @GuiguLogin
    @PostMapping("savePaidRecords")
    public Result savePaidRecords(@RequestBody UserPaidRecordVo userPaidRecordVo){
//        userInfoService.savePaidRecords(userPaidRecordVo);
        PaidRecordStrategy paidRecordStrategy = paidRecordContext
                .getPaidRecordStrategy(userPaidRecordVo.getItemType());
        paidRecordStrategy.savePaidRecords(userPaidRecordVo);
        return Result.ok(true);
    }
    @Operation(summary = "内部接口：保存wx支付成功的交易记录")
    @PostMapping("saveWxPaidRecords")
    public Result saveWxPaidRecords(@RequestBody UserPaidRecordVo userPaidRecordVo){
        PaidRecordStrategy paidRecordStrategy = paidRecordContext
                .getPaidRecordStrategy(userPaidRecordVo.getItemType());
        paidRecordStrategy.savePaidRecords(userPaidRecordVo);
        return Result.ok(true);
    }

    @Operation(summary = "内部接口：查询用户是否购买过专辑")
    @GuiguLogin
    @GetMapping("isBuyAlbum/{albumId}")
    public Result<Boolean> isBuyAlbum(@PathVariable("albumId")Long albumId){
        long count = userPaidAlbumService.count(
                Wrappers.lambdaQuery(UserPaidAlbum.class)
                        .eq(UserPaidAlbum::getAlbumId, albumId)
                        .eq(UserPaidAlbum::getUserId, AuthContextHolder.getUserId())
        );
        return Result.ok(count>0);
    }
    ///api/user/userInfo/isSubscribe/936
    @Operation(summary = "根据专辑id查询是否订阅专辑")
    @GuiguLogin
    @GetMapping("/isSubscribe/{albumId}")
    public Result isSubscribe(@PathVariable("albumId")Long albumId){
        long count = userSubscribeService.count(Wrappers.lambdaQuery(UserSubscribe.class)
                .eq(UserSubscribe::getAlbumId, albumId)
                .eq(UserSubscribe::getUserId, AuthContextHolder.getUserId()));
        return Result.ok(count>0);
    }

    @Operation(summary = "内部调用：根据用户id和专辑id查询购买过的声音id列表")
    @GetMapping("getPaidTrackIds/{userId}/{albumId}")
    public Result<Set<Long>> getPaidTrackIds(@PathVariable("userId")Long userId ,
                                             @PathVariable("albumId")Long albumId){
        Set<Long> paidTrackIds = userPaidTrackService
                .list(Wrappers.lambdaQuery(UserPaidTrack.class)
                        .eq(UserPaidTrack::getUserId, userId)
                        .eq(UserPaidTrack::getAlbumId, albumId)
                        .select(UserPaidTrack::getTrackId))
                .stream()
                .map(UserPaidTrack::getTrackId).collect(Collectors.toSet());
        return Result.ok(paidTrackIds);

    }

    @Operation(summary = "内部调用：根据用户id查询主播信息")
    @GetMapping("getUserInfo/{id}")
    public Result<UserInfoVo> getUserInfo(@PathVariable("id")Long id){
        UserInfo userInfo = userInfoService.getById(id);
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo,userInfoVo);
        return Result.ok(userInfoVo);
    }
}

